window.Muni = window.Muni || {};
Muni.Map = Muni.Map || {};
Muni.Marker = Muni.Marker || {};


var defaultMapOpt = {
	id: 'map-div',
	zoom: 1,
	latitude: 43.23,
	longitude: 34.23,
    draggable: true,
    maxZoom: 13,
    minZoom: 3,
    mapType: 'ROADMAP',
    panControl: true,
    mapTypeControl: true,
    scaleControl: true,
    zoomControl: true
};

var prepareOptions = function(cfg){
    var options = {};
    $.extend(options, defaultMapOpt, cfg);
    return options;    
};

var resolveMapType = function(server, mapType){
    switch (server){
        case 'google':
            if(mapType == 'ROADMAP'){
                return google.maps.MapTypeId.ROADMAP; 
            }
            if(mapType == "SATELLITE"){
                return google.maps.MapTypeId.SATELLITE;
            }
            if(mapType == "HYBRID"){
                return google.maps.MapTypeId.HYBRID;
            }
            if(mapType == 'TERRAIN'){
                return google.maps.MapTypeId.TERRAIN;
            }
            break;
        case 'seznam':
            if(mapType == 'ROADMAP'){
                return SMap.DEF_BASE;
            }
            if(mapType == 'SATELLITE'){
                return SMap.DEF_OPHOTO;
            }
            if(mapType == 'TERRAIN'){
                return SMap.DEF_TURIST;
            }
            //hybrid doesn't work right now
            if(mapType == 'HYBRID'){
                return SMap.DEF_HYBRID;
            }
            break;
        case 'bing':
            if(mapType == 'ROADMAP'){
                return Microsoft.Maps.MapTypeId.Road;
            }
            if(mapType == 'SATELLITE'){
                return Microsoft.Maps.MapTypeId.Birdseye;
            }
            if(mapType == 'TERRAIN'){
                return Microsoft.Maps.MapTypeId.Aerial;
            }
            if(mapType == 'HYBRID'){
                return Microsoft.Maps.MapTypeId.Mercator;
            }
    }
};


Muni.Map.google = (function(){
    var map;

    var convertToGmapOptions = function(options){
        var latLng = new google.maps.LatLng(options.latitude, options.longitude);
        var mapType = resolveMapType('google', options.mapType);
        var gmapOptions = {
            zoom: options.zoom,
            center: latLng,
            draggable: options.draggable,
            scrollwheel: options.draggable,
            mapTypeId: mapType,
            maxZoom: options.maxZoom,
            minZoom: options.minZoom,
            panControl: options.panControl,
            zoomControl: options.zoomControl,
            mapTypeControl: options.mapTypeControl,
            scaleControl: options.scaleControl
        }			
        return gmapOptions;
    }

    return {
        setMap: function(cfg){
            var options = prepareOptions(cfg);	            
            var gmapsOptions = convertToGmapOptions(options);
            map = new google.maps.Map(document.getElementById(options.id), gmapsOptions);                    
            return map;
        },

        getMap: function(){
            return map;
        },
    
    }

})();

Muni.Map.seznam = (function(){
    var map;
	
    return {
		setMap: function(cfg){
            var options = prepareOptions(cfg);
			var center = SMap.Coords.fromWGS84(options.latitude, options.longitude);
            var mapType = resolveMapType('seznam', options.mapType);
            var sOptions = {
                minZoom: options.minZoom,
                maxZoom: options.maxZoom
            };
			map = new SMap(document.getElementById(options.id), center, options.zoom, sOptions);
            if(!options.draggable){
                map.lock();
            }
            map.addDefaultLayer(mapType).enable();		
            if(options.panControl){
                map.addControl(new SMap.Control.Compass());
            }
            if(options.zoomControl){
                map.addControl(new SMap.Control.Zoom())
            }
            if(options.mapTypeControl){
                var mapTypeControl = new SMap.Control.Layer();
                mapTypeControl.addLayer(SMap.DEF_BASE, "Roadmap", title="Roadmap");
                mapTypeControl.addLayer(SMap.DEF_OPHOTO, "Satellite", title="Satellite");
                mapTypeControl.addLayer(SMap.DEF_TURIST, "Terrain", title="Terrain");
                mapTypeControl.addLayer(SMap.DEF_HYBRID, "Hybrid", title="Hybrid");
                map.addControl(mapTypeControl);
            }
            
            return map;
		},

        getMap: function(){
            return map;
        }

    }   			
})();

Muni.Map.bing = (function(){
    var map;

    return {
        setMap: function(cfg, key){
            var options = prepareOptions(cfg);
            var coords = new Microsoft.Maps.Location(options.latitude, options.longitude);
            var mapType = resolveMapType('bing', options.mapType);
            var bOptions = {
                credentials: key,
                center: coords,
                zoom: options.zoom,
                disableMouseInput: !options.draggable,
                showDashboard: options.panControl || options.zoomControl,
                showScalebar: options.scaleControl,
                showMapTypeSelector: options.mapTypeControl
            };

            map = new Microsoft.Maps.Map(document.getElementById(options.id), bOptions);           
            return map; 
        },

        getMap: function(cfg){
            return map;
        }
    };
})();



